package com.wuye.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.wuye.common.dao.hibernate.BaseDaoHibernate;
import com.wuye.common.util.numeric.NumericUtil;
import com.wuye.common.util.string.StrUtil;
import com.wuye.common.vo.PageInfo;
import com.wuye.constants.BaseConstants;
import com.wuye.dao.UserDao;
import com.wuye.entity.Community;
import com.wuye.entity.Organization;
import com.wuye.entity.Privilege;
import com.wuye.entity.PropertyCompany;
import com.wuye.entity.RolePrivilege;
import com.wuye.entity.User;
import com.wuye.entity.UserAuth;

@Repository("userDao")
public class UserDaoImpl extends BaseDaoHibernate implements UserDao {
	public User getUserByAccountAndPwd(String account, String pwd) {

		String sql="select a.* from user a  where a.account=? and a.pwd=? ";
		List list=new ArrayList();
		list.add(account);
		list.add(pwd);
		PageInfo pageInfo=super.findPageInfoByJDBCSQLAndParams(sql, list, 1, 1, false, User.class);
		if(pageInfo!=null&&pageInfo.getDataList()!=null
				&&pageInfo.getDataList().size()>0){
			User user=(User)pageInfo.getDataList().get(0);
			return user;
		}else{
			return null;
		}
	}

	public User getUserByAccount(String account) {
		String sql = "from User u where u.account  = ? ";
		List params = new ArrayList();
		params.add(account);
		List<User> userList = this.findListByHQLAndParams(sql, params);
		if (!StrUtil.isNullOrEmpty(userList) && userList.size() > 0) {
			return userList.get(0);
		}
		return null;
	}

	public PageInfo getUser(Map<String, Object> map, int currentPage,
			int perPageNum) {
		PageInfo retPageInfo = null;
		StringBuffer hql = new StringBuffer();
		List params = new ArrayList();
		if (map == null) {
			return null;
		}
		String qryType = StrUtil.strnull(map.get("qryType"));
		//用户登录关联的orgId
		int orgId = NumericUtil.nullToIntegerZero(map.get("orgId"));
		User qryUser = (User)map.get("qryUser");
		StringBuffer qry = new StringBuffer();
				
		if (qryType.equals("company")){
			//根据物业公司查询
			if (!StrUtil.isNullOrEmpty(map.get("companyId"))){
				PropertyCompany company = (PropertyCompany) PropertyCompany.getDao().getObject(PropertyCompany.class, NumericUtil.nullToIntegerZero(map.get("companyId")));
				if (company == null){
					return null;
				}
				
				/*String companyName = StrUtil.strnull(map.get("companyName"));
				hql.setLength(0);
				params.clear();
				hql.append("select a from User a, PropertyCompany b where a.ownerCompany = b and b.companyName like ? ");
				params.add("%"+companyName+"%");*/
				hql.setLength(0);
				params.clear();
				hql.append("select a from User a where a.ownerCompany = ? ");
				params.add(company);
				return super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
			}else if (!StrUtil.isNullOrEmpty(map.get("companyName"))){
				PropertyCompany company = PropertyCompany.getDao().getCompanyByName(StrUtil.strnull(map.get("companyName")));
				if (company != null){
					hql.setLength(0);
					params.clear();
					hql.append("select a from User a where a.ownerCompany.companyId = ? ");
					params.add(company.getId());
					return super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
				}
			}
			return null;
		}else if(qryType.equals("community")){
			//根据小区查询
			if (!StrUtil.isNullOrEmpty(map.get("communityId"))){
				Community community = (Community) Community.getDao().getObject(Community.class, NumericUtil.nullToIntegerZero(map.get("communityId")));
				if (community == null){
					return null;
				}
				Organization comOrg = community.getOrganization();
				if (comOrg != null){
					hql.setLength(0);
					hql.append("select a from User a, UserOrgRel b ");
					hql.append(" and a.userId = b.user.userId ");
					hql.append(" and b.organization.orgId = ? ");
					params.clear();
					params.add(orgId);
					return super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
				}
				/*String companyName = StrUtil.strnull(map.get("companyName"));
				hql.setLength(0);
				params.clear();
				hql.append("select a from User a, PropertyCompany b where a.ownerCompany = b and b.companyName like ? ");
				params.add("%"+companyName+"%");
				hql.setLength(0);
				params.clear();
				hql.append("select a from User a where a.ownerCompany = ? ");
				params.add(company);
				return super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);*/
			}else if (!StrUtil.isNullOrEmpty(map.get("communityName"))){
				
			}
		}else if (qryType.equals("name")){
			//根据名字查询
			String partyName = StrUtil.strnull(map.get("partyName"));
			if (!partyName.equals("")){
				hql.setLength(0);
				params.clear();
				if (BaseConstants.USER_TYPE_10.equals(qryUser.getUserType())){
					//平台用户查询所有
					hql.append(" select u from User u, PartyInfo p ")
					.append("where u.userId=p.objId ")
					.append(" and p.classId = ? ")
					.append(" and p.partyName like ? ");
					params.add(BaseConstants.CLASS_USER);
					params.add("%"+partyName+"%");
				}else{
					if (orgId == 0){
						//非平台用户只能查管理范围的用户
						hql.setLength(0);
						hql.append("select a from User a, PartyInfo p , UserOrgRel b ")
						.append(" where a.userId = b.user.userId ")
						.append(" and (b.organization.upOrgId = ? or b.organization.orgId = ?) ")
						.append(" and a.userId=p.objId ")
						.append(" and p.classId = ? ")
						.append(" and p.partyName like ? ");
						
						params.clear();
						params.add(orgId);
						params.add(orgId);
						params.add(BaseConstants.CLASS_USER);
						params.add("%"+partyName+"%");
					}else{
						return null;
					}
				}
				
				return super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
			}
			return null;
		}else if (qryType.equals("user")){
			String account = StrUtil.strnull(map.get("account"));
			if (!account.equals("")){
				hql.setLength(0);
				params.clear();
				if (BaseConstants.USER_TYPE_10.equals(qryUser.getUserType())){
					//平台用户查询所有
					hql.append(" select u from User u ")
					.append(" where 1=1 ")
					.append(" and u.account = ? ");
					params.add(account);
				}else{
					if (orgId == 0){
						//非平台用户只能查管理范围的用户
						hql.setLength(0);
						hql.append("select a from User a,UserOrgRel b ")
						.append(" where a.userId = b.user.userId ")
						.append(" and (b.organization.upOrgId = ? or b.organization.orgId = ?) ")
						.append(" and a.account = ? ");
						
						params.clear();
						params.add(orgId);
						params.add(orgId);
						params.add(account);
					}else{
						return null;
					}
				}
				
				return super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
			}
		}else if (qryType.equals("all")){
			hql.setLength(0);
			params.clear();
			if (BaseConstants.USER_TYPE_10.equals(qryUser.getUserType())){
				//平台用户查询所有
				hql.append(" select u from User u ")
				.append(" where 1=1 ");
			}else{
				if (orgId != 0){
					//非平台用户只能查管理范围的用户
					hql.setLength(0);
					hql.append("select distinct a from User a,UserOrgRel b ")
					.append(" where a = b.user ")
					.append(" and (b.organization.upOrgId = ? or b.organization.orgId = ?) ");
					
					params.clear();
					params.add(orgId);
					params.add(orgId);
				}else{
					return null;
				}
			}
			return super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
		}else{
			//根据userId查询
			hql.setLength(0);
			params.clear();
			
			hql.append(" select u from User u ")
			.append(" where 1=1 ")
			.append(" and u.statusCd <> '1100' ");
			
			if (!StrUtil.isNullOrEmpty(map.get("userId"))){
				hql.append(" and u.userId = ? ");
				params.add(NumericUtil.nullToIntegerZero(map.get("userId")));
			}
			if (!StrUtil.isNullOrEmpty(map.get("account"))){
				hql.append(" and u.account = ? ");
				params.add(NumericUtil.nullToIntegerZero(map.get("account")));
			}
			if (!StrUtil.isNullOrEmpty(map.get("userType"))){
				hql.append(" and u.userType = ? ");
				params.add(NumericUtil.nullToIntegerZero(map.get("userType")));
			}
			
			return super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
		}
		return null;
	}
	
	public PageInfo getUser(PropertyCompany company,int currentPage,int perPageNum){
		List<Organization> orgs = new ArrayList<Organization>();
		List<Organization> childOrgs = company.getOrganization().getAllChild();
		if (childOrgs != null && childOrgs.size() > 0){
			orgs.addAll(childOrgs);
		}
		orgs.add(company.getOrganization());
		StringBuffer hql = new StringBuffer();
		hql.append(" select a from User a, UserOrgRel b")
			.append(" where 1=1")
			.append(" where a.userId= b.userId")
			.append(" and b.orgId in (?)");
		List<Integer> orgIds = new ArrayList<Integer>();
		for (Organization org : orgs){
			orgIds.add(org.getId());
		}
		List<Object> params = new ArrayList<Object>();
		params.add(orgIds.toArray(new Integer[0]));
		return super.findPageInfoByJDBCSQLAndParams(hql.toString(), params, currentPage, perPageNum);
	}

	public List<User> getUserByOpendId(String opendId) {
		String sql = "select * from user where opend_id = ? ";
		List params = new ArrayList();
		params.add(opendId);
		List<User> userList = this.jdbcFindList(sql, User.class, params);
		return userList;
	}

	public <E> PageInfo jdbcFindPageInfo(String sql, Class<E> elementType,
			List<Object> params, int currentPage, int perPageNum) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean hasPrivilege(User user, String privilegeCode) {
		return user.hasPrivilege(privilegeCode);
	}



	public List<Privilege> getPrivileges(User user, boolean isDirectPrivilege) {
		if (user == null ){
			return null;
		}
		if (user.getUserAuths() != null && user.getUserAuths().size() > 0){
			List<Privilege> list = new ArrayList<Privilege>();
			for (Object tmpObj : user.getUserAuths()){
				UserAuth auth = (UserAuth)tmpObj;
				if (auth.getRole() != null  && !isDirectPrivilege){
					Set<RolePrivilege> rolePrivileges = auth.getRole().getRolePrivileges() ;
					if (rolePrivileges != null && rolePrivileges.size() > 0){
						for (RolePrivilege tmpPri : rolePrivileges){
							if (tmpPri.getPrivilege() != null){
								list.add(tmpPri.getPrivilege());
							}
						}
					}
				}else{
					if (auth.getPrivilege() != null){
						list.add(auth.getPrivilege());
					}
				}
			}
			return list;
		}else{
			return null;
		}
	}
}
